﻿using UnityEngine;
using System.Collections;

public class IntroScene : MonoBehaviour
{
	// Properties

	[SerializeField]
	GameObject			m_titleScreen;
	[SerializeField]
	GameObject			m_accessDenied;

	[SerializeField]
	TextList			m_menuList;



	// Members

	enum Page
	{
		Title,
		AccessDenied,
	}


	Page				m_currentPage	= Page.Title;




	void Awake()
	{
		Random.seed	= System.DateTime.Now.Millisecond;		// 랜덤 시드

		//TEST
		//CodexManager.clearCount	= 2;

		SetupMenu();
	}

	void SetupMenu()
	{
		int curLoop		= CodexManager.clearCount + 1;
		string [] menus	= null;

		if(curLoop <= 1)							// * 루프 한번도 돌지 않았을 때
		{
			menus		= new string[] { "Play",
										 "???",
										"MasterPassword"};
		}
		else
		{
			menus		= new string[] { "Play Loop #" + curLoop,
										"Database",
										"MasterPassword",
										"RESET" };
		}

		m_menuList.SetList(menus);
	}

	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Return))		// * 엔터를 눌렀을 시 메뉴 항목 처리
		{
			switch(m_currentPage)
			{
			case Page.Title:
				ProcessMenu();
				break;
				
			case Page.AccessDenied:
				ChangePage(Page.Title);
				break;
			}
		}
	}

	void ProcessMenu()
	{
		int menuInd		= m_menuList.currentIndex;

		switch(menuInd)
		{
		case 0:		// * 게임 시작

			GameScene.stageDataToLoad	= StageInfos.stage1;
			Application.LoadLevel("GameScene");
			break;
		case 1:		// * 데이터베이스(코덱스 씬)
			if(CodexManager.clearCount > 0)				// * 2회차 플레이부터 개방
			{
				Application.LoadLevel("CodexScene");
			}
			else
			{
				ChangePage(Page.AccessDenied);
			}
			break;
		case 2:		// * 마스터 패스워드
			if(!CodexManager.masterPass)
				Application.LoadLevel("MasterPassScene");
			break;
		case 3:
			CodexManager.clearCount	= 0;
			CodexManager.codexPoint	= 0;
			CodexManager.masterPass	= false;
			Application.LoadLevel("IntroScene");
			break;
		}
	}

	void ChangePage(Page pageTo)
	{
		m_titleScreen.SetActive(false);
		m_accessDenied.SetActive(false);

		m_currentPage	= pageTo;
		switch(pageTo)
		{
		case Page.Title:
			m_titleScreen.SetActive(true);
			break;

		case Page.AccessDenied:
			m_accessDenied.SetActive(true);
			break;
		}
	}


	// TEST : 데이터 소거
#if UNITY_EDITOR
	void OnGUI()
	{
		if(GUI.Button(new Rect(0, 0, 150, 50), "ClearData"))
		{
			CodexManager.clearCount	= 0;
			CodexManager.codexPoint	= 0;
			CodexManager.masterPass	= false;
		}

		if(GUI.Button(new Rect(0, 50, 150, 50), "Set Data"))
		{
			CodexManager.clearCount	= 4;
			CodexManager.codexPoint	= 100;
			CodexManager.masterPass	= false;
		}
	}
#endif
}
